package net.jacqeush.mini.rpc.client.listener;

import io.netty.channel.ChannelFuture;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.jacqeush.mini.rpc.cache.ConnectCache;
import net.jacqeush.mini.rpc.handler.AvailableAndWaitHandler;
import net.jacqeush.mini.rpc.handler.RpcClientHandler;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Jacquesh
 * @version 1.0
 * @date 2020/10/16 0:43
 */

@Slf4j
@Getter
@Setter
public class ChannelSimpleListener implements ChannelListener {
    private   ReentrantLock connectedLock = new ReentrantLock();
    private  Condition connectedCondition = connectedLock.newCondition();
    private long connectTimeoutMills=6000;

    @Override
    public void operationComplete(ChannelFuture future) {
     if (future.channel().isActive()){
        log.info("channel is succeed connect ,the channel net address is:"+future.channel().remoteAddress());
         RpcClientHandler handler = future.channel().pipeline().get(RpcClientHandler.class);
         if (handler.getSuccess()){
             ConnectCache.getInstance().cache(handler);
             AvailableAndWaitHandler.available();
         }

    }

    }

}
